let joblistModule = (() => {
  let
    tbody = $('tbody');

  // 获取list并渲染
  let render_job = async function () {
    let
      res = await axios.get('/job/list'),
      str = ``;
    if (res.code) {
      alert('获取职务列表失败')
      return
    }

    //渲染
    res.data.forEach(item => {
      str += `
        <tr>
          <td class="w8">${item.id}</td>
          <td class="w10">${item.name}</td>
          <td class="w20">${item.desc}</td>
          <td class="w50">${item.power}</td>
          <td class="w12" jobId="${item.id}">
            <a href="javascript:;">编辑</a>
            <a href="javascript:;">删除</a>
          </td>
			  </tr>
      `
    })
    tbody.html(str)
  };
  let bindDelegate = function () {
    tbody.click(async ev => {
      let
        target = ev.target,
        TAG = target.tagName,
        content = target.innerHTML,
        jobId = $(target.parentElement).attr('jobId');
      if (TAG == 'A') {
        if (content == '编辑') {
          window.location.href = `jobadd.html?jobId=${jobId}`;
        }
        if (content == '删除') {
          let flag = confirm(`确定要删除id为${jobId}的部门吗？`)
          if (flag) {
            let res = await axios.get('/job/delete', { params: { jobId } })
            !res.code ? (alert('删除部门信息成功'), render_job()) : alert('删除失败')
          }
        }
      }
    })
  };
  return {
    init() {
      render_job();
      // 绑定事件委托
      bindDelegate();
    }
  }
})();

joblistModule.init();